package com.cat.test.test01;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;

/**
 * @author 曲大人的喵
 * @description
 * @create 2025/10/28 12:55
 * @since JDK17
 */

public class Solution07 {

    public static void main(String[] args) {
        // groupingBy : 处理分组问题
        Stream<String> stream = Stream.of("令狐冲", "风清扬", "独孤求败", "方证",
                "东方不败", "冲虚", "向问天", "任我行", "不戒");
        // 1. 根据长度来分组
        Map<Integer, List<String>> collect = stream.collect(groupingBy(String::length, Collectors.toList()));
        collect.entrySet().forEach(System.out::println);
        Map<Integer, List<Integer>> map = IntStream.range(1, 11).mapToObj(Integer::valueOf).
                collect(groupingBy(x -> x % 2, Collectors.toList()));    // 函数式编程
        map.entrySet().forEach(System.out::println);
        // 分组
        Stream<Hero> heroes = Stream.of(
        new Hero( "令狐冲", 90),
        new Hero("风清扬", 98),
        new Hero("独孤求败", 100),
        new Hero("方证", 92),
        new Hero("东方不败", 98),
        new Hero("冲虚", 90),
        new Hero("向问天",89),
        new Hero("任我行", 92),
        new Hero("不戒", 88));
//        Map<Integer, List<Hero>> h = heroes.collect(Collectors.groupingBy(x -> x.name().length(), Collectors.toList()));
//        h.entrySet().forEach(System.out::println);
//        // mapping(映射, 下游收集器)
//        heroes.collect
//                (Collectors.groupingBy(x -> x.name().length(), Collectors.mapping(Hero::strength, Collectors.toList()))).
//                entrySet().forEach(System.out::println);
//
//        heroes.collect
//                (Collectors.groupingBy(x -> x.name().length(),
//                        Collectors.filtering(x -> x.strength() < 90, Collectors.toList()))).
//                entrySet().forEach(System.out::println);
//        Map<Integer, List<Object>> h = heroes.collect
//                (groupingBy(x -> x.name().length(),
//                        flatMapping(x -> x.name().chars().mapToObj(Character::toString), toList())));
//        h.entrySet().forEach(System.out::println);
//        Map<Integer, Long> h = heroes.collect(groupingBy(x -> x.name().length(), counting()));
//        h.entrySet().forEach(System.out::println);
        Map<Integer, Optional<Hero>> h = heroes.collect(groupingBy(x -> x.name().length(), minBy(Comparator.comparing(Hero::strength))));
        h.entrySet().forEach(System.out::println);


    }
}
